﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PloobsEngine.Utils;
using PloobsEngine.SceneControl;
using PloobsEngine.Behaviors;

namespace PloobsEngine.IA
{
    public class SideActuator : IActuator
    {
        public SideActuator(IObject character)
        {
            this.obj = character;
            this.Name = "SideActuator ";
            resp.ActuatorName = Name;
        }

        public void Init()
        {
            this.front = obj.PhysicObject.FaceVector;
            side = rd.Next() % 2;
        }

        int side = -1;
        Random rd= new Random();
        private Vector3 front;
        private IObject obj;
        private float angleToTurn = 70;

        public float AngleToTurn
        {
            get { return angleToTurn; }
            set { angleToTurn = value; }
        }
        
        ActuatorSensorInformation resp = new ActuatorSensorInformation();               

        public override ActuatorSensorInformation PerformAct()
        {

            double x = VectorUtils.FindAngleBetweenTwoVectors2D(front,obj.PhysicObject.FaceVector);

            if (Math.Abs(x) < MathHelper.ToRadians(angleToTurn) || x.Equals(double.NaN))
            {           
            if(side == 0)
                {
           
                    obj.Behavior.StartBehavior(CharacterMovimentationBehavior.MOVERIGHT, CharacterMovimentationBehavior.MOVERIGHT,null);
                }
             else
                {
                    obj.Behavior.StartBehavior(CharacterMovimentationBehavior.MOVELEFT, CharacterMovimentationBehavior.MOVELEFT, null);
                }                
            }
            else
            {
                obj.Behavior.StartBehavior(CharacterMovimentationBehavior.MOVEFRONT, CharacterMovimentationBehavior.MOVEFRONT, null);
             
            }
            resp.Ended = false;
            return resp;
        }
    }
}
